/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.ipfw.cs590.cheat.struts.actions;

import edu.ipfw.cs590.cheat.AssignmentManagementInterface;
import edu.ipfw.cs590.cheat.AssignmentManagementService;
import edu.ipfw.cs590.cheat.struts.actionforms.AssignmentSearchForm;
import edu.ipfw.cs590.cheat.util.Authentication;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.web_cat._1_0.assignment_sharing.Assignment;

/**
 *
 * @author SafarTimuro
 */
public class getMyAssignments extends org.apache.struts.action.Action {

    private final static String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        //Check priveleges
        if (!Authentication.isTeacher(request.getSession(false))) {
            //Something bad happened on the webservice side fail.
            ActionMessages errors = new ActionMessages();
            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.service.access.insufficient"));
            this.saveErrors(request, errors);

            return mapping.getInputForward();
        }

        //If we have a valid form
        if (form instanceof AssignmentSearchForm) {
            AssignmentSearchForm asf = (AssignmentSearchForm) form;

            try {
                //Setup webservice
                AssignmentManagementService service = new AssignmentManagementService();
                AssignmentManagementInterface port = service.getAssignmentManagementSOAP();
                List<Assignment> searchResult = new ArrayList<Assignment>();

                //Get the search by author's email
                searchResult = port.findAssignmentByAuthor(
                        (String) request.getSession(false).getAttribute(
                        Authentication.SESSION_NAME_EMAIL));

                //Successfull search
                asf.setSearchResults(searchResult);
                return mapping.findForward(SUCCESS);
            } catch (Exception exc) {
                //Something bad happened on the webservice side fail.
                ActionMessages errors = new ActionMessages();
                errors.add(ActionMessages.GLOBAL_MESSAGE,
                        new ActionMessage("errors.service.error", "Get My Assignments", exc.getMessage()));
                this.saveErrors(request, errors);

                //Return failure.
                return mapping.getInputForward();
            }
        }

        //Something bad happened on the webservice side fail.
        ActionMessages errors = new ActionMessages();
        errors.add(ActionMessages.GLOBAL_MESSAGE,
                new ActionMessage("error.form.invalid"));
        this.saveErrors(request, errors);

        return mapping.getInputForward();
    }
}
